import {
    videoUrl,
    getCrouseDetail,
} from '../../api/article'
import {
    getId,
    submit,
    pay

} from '../../api/user'
// pages/paly/paly.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: '',
        list: [],
        isshow: true,
        isPlay: false,
        courseId: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        console.log(options)
        this.init(options)
    },
    async init(options) {
        const res = await getCrouseDetail({
            courseId: options.id
        })
        this.setData({
            list: res.content.chapterList,
            isshow: wx.getStorageSync('isSimpleCourse'),
            content: res.content.content,
            courseId: res.content.courseId
        })
        if ((res.content.price == 0 && res.content.vipPrice == 0) || res.content.flag == 0) {
            this.setData({
                isPlay: false
            })
        } else {
            this.setData({
                isPlay: true
            })
        }
    },
    async handlePay() {
        if (!wx.getStorageSync('token')) {
            wx.showModal({
                title: '提示',
                content: '请登录后购买',
                complete: (res) => {
                    if (res.cancel) {

                    }
                    if (res.confirm) {
                        wx.switchTab({
                            url: '/pages/my/my',
                        })
                    }
                }
            })
            return
        }
        const ids = await getId({
            type: 2
        })
        const res = await submit({
            goodsId: this.data.courseId,
            num: 1,
            orderId: ids.content
        })
        const payres = await pay({
            orderId: ids.content
        })
        if (payres.content) {
            wx.requestPayment({
                nonceStr: payres.content.nonce_str,
                package: payres.content.package,
                paySign: payres.content.sign,
                signType: 'MD5',
                timeStamp: payres.content.timestamp,
                success(res) {
                    console.log(res)
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success'
                    })
                    this.init({
                        courseId: this.data.courseId
                    })
                },
                fail(err) {
                    console.log(err)
                    wx.showToast({
                        title: '支付失败',
                        icon: 'error'
                    })
                }
            })
        }
    },
    async handlePlay(e) {
        console.log(e)
        const res = await videoUrl({
            videoId: e.currentTarget.dataset.item.videoId
        })
        if (res.content) {
            this.setData({
                url: res.content
            })
            wx.showToast({
                title: `播放${e.currentTarget.dataset.item.title}`,
                icon: 'none'
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

})